import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:my_demo_flutter/pages/main_page.dart';
import 'generated/l10n.dart';
import 'init_configs.dart';
import 'my_base_lib/base_lib.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]) //固定竖屏
      .then((value) {
    AppConfigs.init();
    runApp(MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.grey,
        fontFamily: 'Gilroy',
        textTheme: const TextTheme(
          bodyLarge: TextStyle(),
          bodyMedium: TextStyle(),
        ).apply(
          bodyColor: const Color(0xff191919),
          displayColor: const Color(0xff191919),
        ),
      ),
      debugShowCheckedModeBanner: false,
      initialRoute: MainPage.routeName,
      onGenerateRoute: onGenerateRoute,
      locale: Locale(LanguageUtils.getLanguageType()),
      supportedLocales: const [
        Locale('en'), //
        Locale('zh'), //
        // 添加其他支持的语言环境
      ],
      localizationsDelegates: const [
        S.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
    ));
  });
}

var onGenerateRoute = (RouteSettings settings) {
  //统一处理
  final String? name = settings.name;
  final Function? pageContentBuilder = AppConfigs.routes[name];
  if (pageContentBuilder != null) {
    if (settings.arguments != null) {
      return MaterialPageRoute(
          builder: (context) =>
              pageContentBuilder(context, arguments: settings.arguments));
    } else {
      return MaterialPageRoute(
          builder: (context) => pageContentBuilder(context));
    }
  }
  return null;
};
